#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<stdlib.h>

int main(int argc, char const *argv[])
{   
    int fd = open("test.txt", O_WRONLY | O_CREAT | O_APPEND, 0644);
    char *msg=malloc(1024);
    if (fd==-1) 
    {
        perror("open");
        return 1;
    }
    pid_t pid = fork();
    if (pid==-1)
    {   
        perror("fork");
        return 1;
    }
    else if (pid==0)
    {
        //子进程执行的代码
        strcpy(msg, "Hello from child\n");
        write(fd, msg, strlen(msg));

    }else{
        //父进程执行的代码
        sleep(1);
        strcpy(msg, "Hello from parent\n");
        write(fd, msg, strlen(msg));
    }

    //父子进程都执行完
    close(fd);
    free(msg);
    if (pid==0)
    {
        printf("Child process is exiting\n");
    }else{
        printf("Parent process is exiting\n");
    }
    
    

    
}
